//
//  XuanZeChengShiViewController.m
//  XiangMu
//
//  Created by  方崇豪 on 15/7/13.
//  Copyright (c) 2015年  方崇豪. All rights reserved.
//
#define IS_IOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8)
#import "XuanZeChengShiViewController.h"

@interface XuanZeChengShiViewController ()<CLLocationManagerDelegate,MBProgressHUDDelegate>
{
    
    
  
     MBProgressHUD *HUD;
}


@end

@implementation XuanZeChengShiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets=NO;
    UIImageView*imageview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 64)];
    imageview.image=[UIImage imageNamed:@"dianchiliang"];
    imageview.userInteractionEnabled=YES;
    [self.view addSubview:imageview];
    
    UILabel*lable=[[UILabel alloc]initWithFrame:CGRectMake(3, 10, kScreenW, 64)];
    lable.text=@"服务城市";
    lable.textAlignment=NSTextAlignmentCenter;
    lable.textColor=[UIColor whiteColor];
    lable.font=[UIFont boldSystemFontOfSize:17];
    [self.view addSubview:lable];
    
    UIButton*fanHuiBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    fanHuiBtn.frame=CGRectMake(10, 30, 20, 20);

    [fanHuiBtn addTarget:self action:@selector(fanhui) forControlEvents:UIControlEventTouchUpInside];
    [fanHuiBtn setBackgroundImage:[UIImage imageNamed:@"duigou"] forState:UIControlStateNormal];
    
    [imageview addSubview:fanHuiBtn];
    
      self.view.backgroundColor=[UIColor grayColor];
   tablview=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreenW, kScreenH-64) style:UITableViewStylePlain];

    tablview.dataSource = self;
    tablview.delegate = self;
    
    [self.view addSubview:tablview];
    [self DingWei];
    
    HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
    HUD.delegate = self;
    HUD.labelText = @"数据加载中...";
    [HUD show:YES];
    
   __block XuanZeChengShiViewController *xuan=self;
    
    [tablview addPullToRefreshWithActionHandler:^{
          [xuan performSelector:@selector(shuaxin) withObject:xuan afterDelay:1];
        
        
    }];
 
}
-(void)shuaxin{
      [self DingWei];
}
-(void)DingWei{
//        [UIApplication sharedApplication].idleTimerDisabled = TRUE;
//    CLLocationManager* locationmanager = [CCLocationManager shareLocation];
//        [locationmanager requestAlwaysAuthorization];        //
//        [locationmanager requestWhenInUseAuthorization];     //
//        locationmanager.delegate = self;
    [self getAllInfo];
    
}
-(void)getAllInfo
{
   
    __weak XuanZeChengShiViewController *wself = self;
    
    [[CCLocationManager shareLocation]getCityhang:^(NSString *ChengShi, NSString *error)
    {
       
        wself.str=ChengShi;
        [tablview reloadData];
        if (ChengShi!=nil) {
            
            [HUD hide:YES];
              [tablview.pullToRefreshView stopAnimating];
            return ;
        }
      
        if (error!=nil) {
            
            [HUD hide:YES];
            
            MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
            hud.mode = MBProgressHUDModeText;
            hud.labelText = @"定位失败请检查网络";
            hud.margin = 10.f;
            
            hud.removeFromSuperViewOnHide = YES;
            
            [hud hide:YES afterDelay:1];
             [tablview.pullToRefreshView stopAnimating];
        }
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
 }
- (void)fanhui{
    
    NSLog(@"12");
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
#pragma mark--UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString*str=@"";
    
    UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:str];
    if (!cell ) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
        
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    if (indexPath.row==0) {
        cell.textLabel.text=@"已开通服务城市";
        cell.textLabel.textColor=[UIColor whiteColor];
        cell.backgroundColor=CL_00cc85;
        
    }
    else{
        cell.textLabel.text=@"杭州";
    }

    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPat{
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 40;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    return [NSString stringWithFormat:@"%@     当前定位城市",_str];
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
